package com.yueya.common.redis;

import com.jfinal.kit.PropKit;
import com.jfinal.plugin.IPlugin;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisPlugin implements IPlugin {
	// protected final Logger logger = Logger.getLogger(getClass());

	private static JedisPool pool;
	private static JedisPoolConfig config;

	public JedisPlugin() {
		config = new JedisPoolConfig();
		config.setMaxIdle(Integer.valueOf(PropKit.get("redis.pool.maxIdle")));
		config.setMaxWaitMillis(Long.valueOf(PropKit.get("redis.pool.maxWait")));
		config.setTestOnBorrow(Boolean.valueOf(PropKit.get("redis.pool.testOnBorrow")));
	}

	@Override
	public boolean start() {
		// TODO Auto-generated method stub
		pool = new JedisPool(config, PropKit.get("redis.ip"), Integer.valueOf(PropKit.get("redis.port")),
				Integer.valueOf(PropKit.get("redis.timeOut")));
		JedisKit.init(pool);
		return true;
	}

	@Override
	public boolean stop() {
		// TODO Auto-generated method stub
		if (pool != null) {
			if (!pool.isClosed()) {
				pool.close();
			}
			pool.destroy();
			return true;
		}

		return false;
	}

}
